package work.net.tcp;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;


/**
 * 使用TCP接收数据
 */
public class Server {
    public static void main(String[] args) {
        System.out.println("服务器启动！");
        try {
            ServerSocket serverSocket = new ServerSocket(9527);
            Socket socket = serverSocket.accept();
            BufferedReader br = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            PrintWriter pw = new PrintWriter(socket.getOutputStream());
            System.out.println("接收客户端消息");

            while (true) {
                String in = br.readLine();
                System.out.println("接收到客户端发来的请求：" + in);
                if ("再见".equals(in)) {
                    break;
                }
                pw.print(in + "回报");
                pw.flush();
            }
        } catch (IOException e) {
            System.out.println("服务启动失败！");
            e.printStackTrace();
        }
    }
}